package com.wargame.engine;

import com.wargame.hexmap.HexagonalMap;
import com.wargame.engine.Judge;
import java.util.HashMap;

/**
 * 被射击的交互方法
 *
 * @param thisOpr 算子对象
 * @paran interact 交互信息
 * @return Object 返回裁决信息，符合裁决模型定义的HashMap<String, Object>类型
 * */
public Object CalloutShootedInteract(Operator thisOpr, Interact interact, HexagonalMap map,HashMap<String, Judge> judgeHandlers) throws ClassNotFoundException {//dynamic interact, Map map, ShootJudgeTables shootJudgeTables){

    int objID = (int) thisOpr.getAttri("ObjID");
    int blood = (int) thisOpr.getAttri("Blood");
    String color = (String) thisOpr.getAttri("Color");
    String subType = (String) thisOpr.getAttri("SubType");
    int changeStateRemainTime = (int) thisOpr.getAttri("ChangeStateRemainTime");
    String targetState = (String) thisOpr.getAttri("TargetState");
    String moveState = (String) thisOpr.getAttri("MoveState");


    int interactObjID = (int) interact.getAttri("ObjID");
    String interactName = (String) interact.getAttri("Name");
    int interactAttObjID = (int) interact.getAttri("AttObjID");
    int interactAttBlood = (int) interact.getAttri("AttBlood");
    String interactAttColor = (String) interact.getAttri("AttColor");
    String interactAttSubType = (String) interact.getAttri("AttSubType");
    String Weapon = (String) interact.getAttri("Weapon");


    boolean isValid = true;
    HashMap<String, Object> judgeInfo;

    if (interactObjID != objID) {
        System.out.println("无效交互：算子不为被交互的算子");
        isValid = false;
    } else if (!interactName.equals("Shooted")) {
        System.out.println("无效交互：交互名称不是“被射击”");
        isValid = false;
    } else if (blood <= 0) {
        System.out.println("无效交互：射击目标" + objID + "血量为零");
        isValid = false;
    }

    if (!isValid) {
        judgeInfo = new HashMap<>();
        judgeInfo.put("IsSuccess", false);
        judgeInfo.put("AttObjID", interactAttObjID);
        judgeInfo.put("AttBlood", interactAttBlood);
        judgeInfo.put("AttColor", interactAttColor);
        judgeInfo.put("AttSubType", interactAttSubType);
//            judgeInfo.put("CurStep", curStep);
        judgeInfo.put("Damage", null);
        judgeInfo.put("Distance", null);
        judgeInfo.put("EleDiff", null);
        judgeInfo.put("OriDamage", null);
        judgeInfo.put("Random1", null);
        judgeInfo.put("Random2", null);
        judgeInfo.put("Random2Rect", null);
        judgeInfo.put("RectDamage", null);
        judgeInfo.put("TargetObjID", objID);
        judgeInfo.put("TargetColor", color);
        judgeInfo.put("TargetSubType", subType);
        judgeInfo.put("Name", "Shoot");
        judgeInfo.put("Weapon", Weapon);
        judgeInfo.put("Keep", null);
        return judgeInfo;
    } else {
        Object[] params = new Object[]{thisOpr, interact, map, judgeHandlers};
        Judge shootJudge = judgeHandlers.get("Shoot");
        judgeInfo = shootJudge.invokeMethod("ToJudgeShoot", params);

        int judgeDamage = (int) judgeInfo.get("Damage");
        boolean judgeKeep = (boolean) judgeInfo.get("Keep");
        if (blood > judgeDamage && judgeDamage>=0){
            blood -= judgeDamage;
        }else {
            blood=0;
            thisOpr.setAttri("IsDead",true);
        }
        thisOpr.setAttri("Blood",blood);
        thisOpr.setAttri("Keep",judgeKeep);
        thisOpr.SetAttri("KeepRemainTime", 151);// 压制倒计时
        if (changeStateRemainTime>0&&targetState.equals("Shelt")){
            thisOpr.SetAttri("ChangeStateRemainTime", 0);// 结束切换状态倒计时
            thisOpr.SetAttri("TargetState",moveState);// 目标状态恢复成当前状态
        }
        return judgeInfo;
    }
}


